package com.annoncesautomobile.vue.adapters;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.annoncesautomobile.R;
import com.annoncesautomobile.vue.MainActivity;
import com.annoncesautomobile.vue.SuperActivity;
import com.annoncesautomobile.vue.actualites.ActualitesActivity;
import com.annoncesautomobile.vue.alertes.AlertesActivity;
import com.annoncesautomobile.vue.autres.AutresActivity;
import com.annoncesautomobile.vue.vendre.VendreActivity;

public class ListeBoutonBas {
	
	public static int indiceButtonSelected = 0;
	
	private static final int darkredColor = Color.parseColor("#B51D1B");

	private static LayoutInflater inflater = null;
	private SuperActivity activity;
	private View view;
	private Button buttonAnnonces;
	private Button buttonVendre;
	private Button buttonAlertes;
	private Button buttonActualites;
	private Button buttonAutres;


	public ListeBoutonBas(final SuperActivity activity) {
		this.activity = activity;
		inflater = (LayoutInflater)this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.view = inflater.inflate(R.layout.bouton_bas_layout2, null);

		// Activite Annonces
		this.buttonAnnonces = (Button) this.view.findViewById(R.id.buttonAnnonces);
		this.buttonAnnonces.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				Intent myIntent = new Intent(v.getContext(), MainActivity.class);
				myIntent.putExtra(MainActivity.EXTRA_NOM_INTENT, MainActivity.INTENT_ANNONCE);
				startActivity(myIntent, MainActivity.class);
			}
		});
		//if(this.activity.getClass() == MainActivity.class) {
		//	this.setButtonSelectedStyle(this.buttonAnnonces);
		//}
		if(indiceButtonSelected == 0) {
			this.buttonAnnonces.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_annonces_darkred, 0, 0);
			this.buttonAnnonces.setTextColor(darkredColor);
		}

		// Activite Vendre
		this.buttonVendre = (Button) this.view.findViewById(R.id.buttonVendre);
		this.buttonVendre.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				Intent myIntent = new Intent(v.getContext(), MainActivity.class);
				myIntent.putExtra(MainActivity.EXTRA_NOM_INTENT, MainActivity.INTENT_VENDRE);
				startActivity(myIntent, VendreActivity.class);
			}
		});
		//if(this.activity.getClass() == VendreActivity.class) {
		//	this.setButtonSelectedStyle(this.buttonVendre);
		//}
		if(indiceButtonSelected == 1) {
			this.buttonVendre.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_vendre_darkred, 0, 0);
			this.buttonVendre.setTextColor(darkredColor);
		}

		// Activite Alertes
		this.buttonAlertes = (Button) this.view.findViewById(R.id.buttonAlertes);
		this.buttonAlertes.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				Intent myIntent = new Intent(v.getContext(), MainActivity.class);
				myIntent.putExtra(MainActivity.EXTRA_NOM_INTENT, MainActivity.INTENT_ALERTE);
				startActivity(myIntent, AlertesActivity.class);
			}
		});
		//if(this.activity.getClass() == AlertesActivity.class) {
		//	this.setButtonSelectedStyle(this.buttonAlertes);
		//}
		if(indiceButtonSelected == 2) {
			this.buttonAlertes.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_alerts_darkred, 0, 0);
			this.buttonAlertes.setTextColor(darkredColor);
		}

		// Activite Actualites
		this.buttonActualites = (Button) this.view.findViewById(R.id.buttonActualites);
		this.buttonActualites.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				Intent myIntent = new Intent(v.getContext(), MainActivity.class);
				myIntent.putExtra(MainActivity.EXTRA_NOM_INTENT, MainActivity.INTENT_ACTUALITE);
				startActivity(myIntent, ActualitesActivity.class);
			}
		});
		//if(this.activity.getClass() == ActualitesActivity.class) {
		//	this.setButtonSelectedStyle(this.buttonActualites);
		//}
		if(indiceButtonSelected == 3) {
			this.buttonActualites.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_actualites_darkred, 0, 0);
			this.buttonActualites.setTextColor(darkredColor);
		}

		// Activite Autres
		this.buttonAutres = (Button) this.view.findViewById(R.id.buttonAutres);
		this.buttonAutres.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				Intent myIntent = new Intent(v.getContext(), MainActivity.class);
				myIntent.putExtra(MainActivity.EXTRA_NOM_INTENT, MainActivity.INTENT_AUTRE);
				startActivity(myIntent, AutresActivity.class);
			}
		});
		//if(this.activity.getClass() == AutresActivity.class) {
		//	this.setButtonSelectedStyle(this.buttonAutres);
		//}
		if(indiceButtonSelected == 4) {
			this.buttonAutres.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_autres_darkred, 0, 0);
			this.buttonAutres.setTextColor(darkredColor);
		}


	}

	public View getView() {	
		return this.view;

	}
	
	protected void setButtonSelectedStyle(Button button) {
		button.setBackgroundResource(R.drawable.button_bas_grey);
	}
	
	protected void setButtonSelectedStyle2(Button button) {
		
	}

	public void startActivity(Intent intent, Class<?> classe) {

		//si on est sur main
		if(this.activity.getClass() == MainActivity.class)
		{
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			this.activity.startActivity(intent);
		}
		else
		{
			if(this.activity.getClass() != classe)
			{
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				this.activity.finish();
				this.activity.startActivity(intent);
			}			
		}
		/*
		if(this.activity.getClass() != classe && classe != MainActivity.class) 
		{
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			// finish l'activite qui appelle l'ouverture d'une autre avec un bouton bas
			if(this.activity.getClass() != MainActivity.class) {
				this.activity.finish();
			}
			this.activity.startActivity(intent);

		}
		 */
	}
}
